package com.woniu.day8.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.day8.mapper.HouseMapper;
import com.woniu.day8.pojo.House;
import com.woniu.day8.pojo.Records;
import com.woniu.day8.pojo.dto.RecordsDto;
import com.woniu.day8.service.RecordsService;
import com.woniu.day8.util.ResultMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/records")
@CrossOrigin
public class RecordsController {
    @Autowired
    private RecordsService recordsService;
    @Autowired
    private HouseMapper houseMapper;

    /**
     * 条件查询
     * @param recordsDto
     * @return
     */
    @RequestMapping("/selectRecords")
    public ResultMessage selectRecords(@RequestBody RecordsDto recordsDto){
        Long pageNum = recordsDto.getPageNum(); // 拿到页码
        Long pageSize = recordsDto.getPageSize(); // 拿到页大小

        Records records = new Records();
        BeanUtils.copyProperties(recordsDto,records);
        Page<Records> page = new Page<>(pageNum, pageSize);
        List<Records> list = recordsService.queryRecords(page, records);

        Map<String, Object> result = new HashMap<>();
        result.put("records", list); // 查询数据
        result.put("total", page.getTotal()); // 数据总条数
        result.put("current", page.getCurrent()); // 当前页
        result.put("pages", page.getPages()); // 总页数
        result.put("size", page.getSize()); // 页大小

        if (list != null && !list.isEmpty()){
            return ResultMessage.success(result);
        }
        return ResultMessage.fail("获取失败");

    }
    /**
     * 添加
     * @param recordsDto
     */
    @RequestMapping("/insertRecords")
    public ResultMessage insertRecords(@RequestBody RecordsDto recordsDto){
        Records records = new Records();
        BeanUtils.copyProperties(recordsDto,records);

        // 如果时间字段为空，则设置为当前时间
        if (records.getUpTime() == null) {
            records.setUpTime(new Date());
        }
        if (records.getOnTime() == null) {
            records.setOnTime(new Date());
        }
        if (records.getCheckTime() == null) {
            records.setCheckTime(new Date());
        }
        Integer i = recordsDto.getHouseName();// 拿到房屋id
        //通过房屋id添加记录
        House house = houseMapper.selectIdByNumbers(i);
        records.setHouseId(house.getId());
        boolean b = recordsService.save(records);
        if (b){
            return ResultMessage.success("添加成功");
        }
        return ResultMessage.fail("添加失败");
    }
    /**
     * 修改
     * @param recordsDto
     */
    @RequestMapping("/updateRecords")
    public ResultMessage updateRecords(@RequestBody RecordsDto recordsDto){
        Records records = new Records();
        BeanUtils.copyProperties(recordsDto,records);
        boolean b = recordsService.updateById(records);
        if (b){
            return ResultMessage.success("修改成功");
        }
        return ResultMessage.fail("修改失败");
    }
    /**
     * 删除
     * @param recordsDto
     */
    @RequestMapping("/deleteRecords")
    public ResultMessage deleteRecords(@RequestBody RecordsDto recordsDto){
        Records records = new Records();
        BeanUtils.copyProperties(recordsDto,records);
        boolean b = recordsService.removeById(records);
        if (b){
            return ResultMessage.success("删除成功");
        }
        return ResultMessage.fail("删除失败");
    }
}
